package itheima.edu.info.manager.controller;

import itheima.edu.info.manager.domain.Teacher;
import itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    //客服
    //开启老师管理系统，并展示老师管理系统菜单
    public void start1() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查询");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统，再见");
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }
    //查看修改方法
    public void updateTeacher() {
        String updateId=inputTeacherId();
        Teacher newTea=inputTeacherInFo(updateId);
        teacherService.updateTeacher(updateId,newTea);
        System.out.println("修改成功");
    }
    //查看删除方法
    public void deleteTeacherById() {
        String delId=inputTeacherId();
        //3.调用业务员中的deleteTeacherById根据id，删除老师
        teacherService.deleteTeacherById(delId);
        //4.提示删除成功
        System.out.println("删除成功");
    }
    //查看老师方法
    public void findAllTeacher() {
        //1.调用业务员中的获取方法，得到老师数组
        Teacher[] teas=teacherService.findAllTeacher();
        //2.判断数组的内存地址是否为null
        if (teas==null){
            System.out.println("查无信息，请添加后重试");
            return;
        }
        //3.遍历学生数组，获取学生数组并打印在控制台上
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teas.length; i++) {
            Teacher tea=teas[i];
            if (tea!=null){
                System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t\t"+tea.getBirthday());
            }
        }
    }
    //添加老师方法
    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师id");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("学号已被占用,请重新输入");
            } else {
                break;
            }
        }
        Teacher tea = inputTeacherInFo(id);
        //	3.将学生对象传递到StudentService中
        boolean result = teacherService.addTeacher(tea);
        //	4.处理StudentService中返回的结果
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
    //键盘录入老师信息
    public Teacher inputTeacherInFo(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师生日");
        String birthday = sc.next();
        Teacher tea = new Teacher();
        tea.setId(id);
        tea.setName(name);
        tea.setAge(age);
        tea.setBirthday(birthday);
        return tea;
    }

    //键盘录入老师id
    public String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入老师id");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入：");
            } else {
                break;
            }
        }
        return id;
    }

}
